package com.gxa.controller.community;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.communitydto.UserApplySearchDto;
import com.gxa.dto.communitydto.UserListDto;
import com.gxa.dto.communitydto.UserListSearchDto;
import com.gxa.entity.community.House;
import com.gxa.entity.community.UserList;
import com.gxa.service.community.UserListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "用户列表的接口")
@RestController
@RequestMapping("/userList")
public class UserListController {

    @Autowired
    private UserListService userListService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "用户列表接口")
    @GetMapping("/list")
    public Result<PageInfo<UserListDto>> queryAll(UserListSearchDto userListSearchDto, Integer pageNum, Integer pageSize){
        PageInfo<UserListDto> userListDtoPageInfo;
        try {
            userListDtoPageInfo = userListService.queryAll(userListSearchDto, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(userListDtoPageInfo);
    }

//    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
//    @ApiOperation(value = "新增用户接口")
//    @PostMapping("/add")
//    public R add(@RequestBody House house){
//        this.list.add(house);
//        return ok("add success").put("添加成功",list);
//    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除用户接口")
    @DeleteMapping("/del")
    public Result del(Integer id){
        try {
            userListService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("删除失败");
        }

        return Result.success();
    }
//    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
//    @ApiOperation(value = "根据id查询用户接口")
//    @GetMapping("/queryById/{id}")
//    public R queryById(@PathVariable("id") Integer id){
//        for(House d:list){
//            if (id == d.getId()){
//                return ok("queryById").put("this",d);
//            }
//        }
//        return error("query error");
//    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "重置用户密码接口")
    @PutMapping("/edit")
    public Result edit(@RequestBody UserList userList){
        try {
            userListService.update(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("重置失败！");
        }
        return Result.success();
    }
}
